package com.phx.serialjni;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.phx.uart.uartJni;

public class MainActivityt extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    byte[] write_buf1 = new byte[] {0x5a, (byte) 0xa5, 0x08, (byte)0x83, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, (byte) 0xff};
    byte[] write_buf2 = new byte[] {0x5a, (byte) 0xa5, 0x08, (byte)0x83, 0x01, 0x02, 0x01, 0x00, 0x01, 0x00, (byte) 0xff};
    byte[] read_buf = new byte[1024];
    int fd;

    Button btn_yule, btn_jiaju, btn_reset;
    TextView  txt_Rx_content;


    Handler handler = new Handler();
    @SuppressLint("HandlerLeak")
    public Handler uHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch(msg.what)
            {
                case 0:
                    break;


                case 1:
                    String commandTxt = msg.getData().getString("alltext");
                    txt_Rx_content.setText(commandTxt);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn_yule = (Button) findViewById(R.id.btn_yule);
        btn_jiaju = (Button) findViewById(R.id.btn_jiaju);
        btn_reset = (Button) findViewById(R.id.btn_reset);


        txt_Rx_content = (TextView) findViewById(R.id.content_Rx);


        btn_yule.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                yulesendbuf();
            }
        });

        btn_jiaju.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                jiajusendbuf();
            }
        });

        btn_reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                reset();
            }
        });
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                int read_count = uartJni.read(fd, read_buf, read_buf.length);
//
//                if (read_count > 0) {
//
//
//                    Message cmdmsg = new Message();
//                    cmdmsg.what = 1;
//                    Bundle bcmd = new Bundle();
//                    String mystr = toHex(read_buf, read_count).toString();
//                    bcmd.putString("alltext", mystr);
//                    uHandler.sendMessage(cmdmsg);
//                }
//                //  txt_Rx_content.setText(toHex(read_buf, read_count));
//
//            }
//        }, 200);
    }



    @Override
    protected void onResume() {
//        String device = "/dev/ttyS3";
//        fd = uartJni.open(device, 115200);
//        Log.d(TAG, "open the device of " + device + ", fd is: " + fd);
        super.onResume();


    handler.postDelayed(new Runnable() {
        String tmpstr = "";
        @Override
        public void run() {


           // int read_count = uartJni.read(fd, read_buf, read_buf.length);
            int read_count = uartJni.read(fd, read_buf, read_buf.length);
            if (read_count < 0) {
                read_count = 0;
            }
            if (read_count > 20) {
                read_count = 20;
            }
            tmpstr = toHex(read_buf, read_count).toString();
            if (!tmpstr.equals("")) {
                Message cmdmsg = new Message();
                cmdmsg.what = 1;
                Bundle bcmd = new Bundle();
                bcmd.putString("alltext", tmpstr);
                cmdmsg.setData(bcmd);
                boolean xx = uHandler.sendMessage(cmdmsg);
            }
         //while
            handler.postDelayed(this,200);
        }  //run

                },1000);

    }

    @Override
    protected void onPause() {
        uartJni.close(fd);
        super.onPause();
    }

    protected void yulesendbuf() {
        int write_count = uartJni.write(fd, write_buf1, write_buf1.length);
        if (write_count < 0) {
            Log.e(TAG, "oncesendbuf: error" );
        }

       // txt_Tx_content.setText(write_buf1[]);
//
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                int read_count = uartJni.read(fd, read_buf, read_buf.length);
//
//                if (read_count > 0) {
//
//
//                    Message cmdmsg = new Message();
//                    cmdmsg.what = 1;
//                    Bundle bcmd = new Bundle();
//                    String mystr = toHex(read_buf, read_count).toString();
//                    bcmd.putString("alltext", mystr);
//                    uHandler.sendMessage(cmdmsg);
//                }
//                //  txt_Rx_content.setText(toHex(read_buf, read_count));
//
//            }
//        }, 200);

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            int read_count = uartJni.read(fd, read_buf, read_buf.length);

            if (read_count < 0) {
                read_count = 0;
            }

            Message cmdmsg = new Message();
            cmdmsg.what = 1;
            Bundle bcmd = new Bundle();
            String tmpstr =  toHex(read_buf, read_count).toString();
             bcmd.putString("alltext", tmpstr);
             cmdmsg.setData(bcmd);
           if(!tmpstr.equals(""))
               uHandler.sendMessage(cmdmsg);
            //  txt_Rx_content.setText(toHex(read_buf, read_count));

        }
    }, 200);

    }

    protected void jiajusendbuf() {
        int write_count = uartJni.write(fd, write_buf2, write_buf2.length);
        if (write_count < 0) {
            Log.e(TAG, "oncesendbuf: error" );
        }

        // txt_Tx_content.setText(write_buf1[]);


        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                int read_count = uartJni.read(fd, read_buf, read_buf.length);

                if (read_count < 0) {
                    read_count = 0;
                }
                txt_Rx_content.setText(toHex(read_buf, read_count));

            }
        }, 100);
    }

    protected void reset() {

        txt_Rx_content.setText(" ");

    }

    public static String toHex(byte[] buffer,int read_count) {
        StringBuffer sb = new StringBuffer(read_count * 2);
        for (int i = 0; i < read_count; i++) {
            sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
            sb.append(Character.forDigit(buffer[i] & 15, 16));
        }
        return sb.toString();
    }


}
